﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace securityLib
{
    public class SecurityMethod
    {

        TripleDESCryptoServiceProvider tripleDES;
        string key_string;
        MD5CryptoServiceProvider md5provider;
        byte[] key;
        UTF8Encoding UTF8;
        public void init()
        {
            tripleDES = new TripleDESCryptoServiceProvider();
            md5provider = new MD5CryptoServiceProvider();
            UTF8 = new UTF8Encoding();
            key_string = "TripDES";
            key = md5provider.ComputeHash(UTF8.GetBytes(key_string));
            

            
            
            //RSAKeyInfo.
        }
        public string encryptRSA(string text)
        {
            RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
            
            return null;
        }
        public string decryptRSA(string text)
        {

            return null;
        }
        public string encrypt(string text)
        {
            tripleDES = new TripleDESCryptoServiceProvider();
            md5provider = new MD5CryptoServiceProvider();
            UTF8 = new UTF8Encoding();
            key_string = "TripDES";
            key = md5provider.ComputeHash(UTF8.GetBytes(key_string));

            tripleDES.Key = key;
            tripleDES.Mode = CipherMode.ECB;
            tripleDES.Padding = PaddingMode.PKCS7;
            byte[] dataEncrypt = UTF8.GetBytes(text);
            byte[] result;
            try
            {
                ICryptoTransform crypto = tripleDES.CreateEncryptor();
                result = crypto.TransformFinalBlock(dataEncrypt, 0, dataEncrypt.Length);
            }
            finally
            {
                tripleDES.Clear();
                md5provider.Clear();
            }
            return Convert.ToBase64String(result);


        }
        public string decrypt(string text)
        {
            tripleDES = new TripleDESCryptoServiceProvider();
            md5provider = new MD5CryptoServiceProvider();
            UTF8 = new UTF8Encoding();
            key_string = "TripDES";
            key = md5provider.ComputeHash(UTF8.GetBytes(key_string));

            tripleDES.Key = key;
            tripleDES.Mode = CipherMode.ECB;
            tripleDES.Padding = PaddingMode.PKCS7;
            byte[] dataDecrypt = Convert.FromBase64String(text);
            byte[] result;

            try
            {
                ICryptoTransform crypto = tripleDES.CreateDecryptor();
                result = crypto.TransformFinalBlock(dataDecrypt, 0, dataDecrypt.Length);

            }
            finally
            {
                tripleDES.Clear();
                md5provider.Clear();
            }
            return UTF8.GetString(result);
        }
    }
}
